﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Kevin.Comm.Utility.Crypto;
using Kevin.Shop.Business.Core.Components.Cookie;
using Kevin.Shop.Business.Core.Member;
using Kevin.Shop.Business.Core.Utility;
using Kevin.Shop.Business.Service.Entity.Core.Member;
using RiderShop.Mobile.Common.Component.OAuth.Weixing;
using RiderShop.Mobile.Common.Helper;
using RiderShop.Mobile.Modle;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Service.Entity.Products.Enumerations;
using System.Collections.Specialized;
using RiderShop.Mobile.Common.Component;
using Kevin.Shop.Business.Comments.Components;
using RiderShop.Mobile.Modle.Purchase;
using RiderShop.Mobile.ServiceEntity.Product;
using Kevin.Shop.Business.Service.Entity.SalesManagement.Shipping;
using Kevin.Shop.Business.Core.Components.Responser;
using RiderShop.Mobile.ServiceEntity.MyAccount;
using Kevin.Shop.Business.SalesManagement.Components.Order;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Modle.MyAccount;
using Kevin.Shop.Business.SalesManagement.Components;
using System.Net;
using System.Web.Script.Serialization;
using RiderShop.Mobile.Common.ThirdEntity;

namespace RiderShop.WebSite.Controllers.MyAccount
{
    public partial class MyAccountController : ThemeController
    {
        [Authorize]
        public ActionResult Home()
        {
            return Redirect("~/MyAccount/Profile");
            //var user = WebClientContext.Current.GetCurrentUser();
            //ViewBag.HeadImgUrl = "";//user.HeadImgUrl;
            //CheckOutModel model = new CheckOutModel();
            ////return View(model);
            ////return ManualView("Home", model); 
            //return View(WebClientContext.Current.SiteTheme, "MyAccount", "Home", "_MobileLayout", model);
        }

        #region MyProfile
        /// <summary>
        /// 打开个人信息界面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult Profile()
        {
            var user = WebClientContext.Current.CurrentUser;
            var customer = new UserProfileModel();
            var user_Cache = WebClientContext.Current.GetCurrentUser(false);
            user.Points = user_Cache != null ? user.Points : 0;

            customer.CurrentUser = user;

            return View(WebClientContext.Current.SiteTheme, "MyAccount", "Profile", WebClientContext.Current.CacheKeyDefaultMasterPage, customer);
        }

        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="collection">个人信息</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public ActionResult Profile(FormCollection collection)
        {
            UserBasicInfo user = WebClientContext.Current.GetCurrentUser();
            user.RealName = collection["RealName"];
            user.Gender = Convert.ToInt32(collection["Gender"]);
            user.BirthDate = Convert.ToDateTime(collection["BirthDate"]);
            user.Email = collection["Email"];
            user.Address = collection["Address"];
            user.CellPhone = collection["CellPhone"];
            ServiceResponser result = MyAccountModel.UpdateUser(user);

            WebClientContext.Current.GetCurrentUser(false);
            var customer = new UserProfileModel();
            var currentUser = WebClientContext.Current.CurrentUser;
            customer.CurrentUser = currentUser;
            customer.Result = result;
            //修改失败,返回页面,页面靠Result显示出失败的信息
            if (customer.Result != null && !customer.Result.Status)
            {
                return View(WebClientContext.Current.SiteTheme, "MyAccount", "Profile", WebClientContext.Current.CacheKeyDefaultMasterPage, customer);
            }

            return Redirect("~/MyAccount/Home");
        }

        /// <summary>
        /// 修改密码适用于表单提交形式
        /// </summary>
        /// <param name="oldPwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        [Authorize]
        public ActionResult ChangePassword(string oldPwd, string newPwd)
        {
            var customer = new CustomerModel();
            if (string.IsNullOrEmpty(oldPwd) && string.IsNullOrEmpty(newPwd))
            {
                return View(WebClientContext.Current.SiteTheme, "MyAccount", "ChangePassword", WebClientContext.Current.CacheKeyDefaultMasterPage, customer);
            }
            customer.ChangePassword(oldPwd, newPwd);
            //修改失败,返回页面,页面靠Result显示出失败的信息
            if (customer.Result != null && !customer.Result.Status)
            {
                return View(WebClientContext.Current.SiteTheme, "MyAccount", "ChangePassword", WebClientContext.Current.CacheKeyDefaultMasterPage, customer);
            }

            return Redirect("~/MyAccount/Home");
        }

        /// <summary>
        /// 修改密码适用于ajax请求形式
        /// </summary>
        /// <param name="oldPwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public JsonResult ChangePasswordByAjaxRequest(string oldPwd, string newPwd)
        {
            var customer = new CustomerModel();
            customer.ChangePassword(oldPwd, newPwd);

            return Json(customer, JsonRequestBehavior.AllowGet);
        }

        [Authorize]
        public ActionResult ChangeTheme(int tmemeType)
        {
            ThemeColorType type = (ThemeColorType)tmemeType;
            KevinCookieManager.Set(CacheKeys.CacheKeySiteTheme, type.ToString());
            return Redirect("~/MyAccount/Profile");
        }
        #endregion

        #region 信任登录（微信）
        public ActionResult TrustedLogin(string openid)
        {
            //ViewBag.OpenId = openid;
            //如果openid为空，跳转到用户注册界面
            if (openid.Trim() == string.Empty)
            {
                return RedirectToAction("Login", "Customer");
            }
            var info = MyAccountModel.GetUserInfoByThirdId(openid);
            //如果openid有绑定的用户，跳转到该用户的用户中心
            if (info != null && info.UserId != 0)
            {
                var model = MyAccountModel.Login(info);
                if (model.Result == null || !model.Result.Status)
                {
                    return RedirectToAction("Profile", "MyAccount");
                }
                return Redirect(model.ReturnUrl);
            }
            return RedirectToAction("BindWeixinAuth", "Customer", new UserBasicInfo { ThirdUserId = openid });
        }
        #endregion
    }
}